package com.codrim.cs.data.server.dao.mapper;

import com.codrim.cs.data.server.dao.entity.StatCsAffOfferDO;
import com.codrim.cs.data.server.dao.entity.SummaryAffiliateData;
import com.codrim.cs.data.server.dao.entity.unique.key.StatCsAffOfferUniqueKey;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author Liang.Zhuge
 * @date 14/07/2017
 */
@Mapper
public interface StatCsAffOfferMapper extends StatCsMapper<StatCsAffOfferDO, StatCsAffOfferUniqueKey> {
    String tableName = "e0000_day_stat_cs_aff_offer";

    @Insert("insert into " + tableName + "(" +
            "sdate, " +
            "symbol, " +
            "affiliate_id, " +
            "offer_id, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc," +
            "single_put_price " +
            ") values (" +
            "#{param.sdate}, " +
            "#{param.symbol}, " +
            "#{param.affiliateId}, " +
            "#{param.offerId}, " +
            "#{param.click}, " +
            "#{param.uclick}, " +
            "#{param.gc}, " +
            "#{param.conversion}, " +
            "#{param.aprice}, " +
            "#{param.pprice}, " +
            "#{param.caps}, " +
            "#{param.sc}," +
            "#{param.singlePutPrice} " +
            ")")
    @Override
    void insert(@Param("param") StatCsAffOfferDO entity,
            @Param("sZone") String sZone,
            @Param("timeDimesion") String timeDimesion);

    @Delete("delete from " + tableName + " where sdate = ${sdate}")
    void deleteSpecifiedDayReportData(@Param("sdate") int sdate, @Param("sZone") String zone);

    @Override
    @Select("<script> select " +
            "id, " +
            "sdate, " +
            "symbol, " +
            "affiliate_id affiliateId, " +
            "offer_id offerId, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc," +
            "single_put_price singlePutPrice " +
            "from " + tableName +
            " where sdate = #{param.sdate} " +
            "<choose>" +
            "<when test='param.offerId !=null'> and offer_id = #{param.offerId} </when>" +
            "<otherwise> and offer_id is null </otherwise>" +
            "</choose>" +
            "<choose>" +
            "<when test='param.affiliateId !=null'> and affiliate_id = #{param.affiliateId} </when>" +
            "<otherwise> and affiliate_id is null </otherwise>" +
            "</choose>" +
            "<choose><when test='param.symbol !=null'> and symbol = #{param.symbol} </when>" +
            "<otherwise> and symbol is null </otherwise></choose>" +
            "<choose><when test='param.singlePutPrice !=null'> and single_put_price = #{param.singlePutPrice} </when>" +
            "<otherwise> and single_put_price = 0 </otherwise></choose>" +
            " limit 1 </script>")
    StatCsAffOfferDO findByKey(@Param("param") StatCsAffOfferUniqueKey key,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Update("update " + tableName + " set " +
            "click = #{value.click}, " +
            "uclick = #{value.uclick}, " +
            "gc = #{value.gc}, " +
            "conversion = #{value.conversion}, " +
            "aprice = #{value.aprice}, " +
            "pprice = #{value.pprice}, " +
            "caps = #{value.caps}, " +
            "sc = #{value.sc} " +
            " where id = #{id}")
    @Override
    void updateById(@Param("value") StatCsAffOfferDO dayStatCsAffOfferDO,
            @Param("id") Integer id,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Select("select " +
            "affiliate_id affiliateId," +
            "symbol, " +
            "sum(click) clicks, " +
            "sum(uclick) uniqueClicks," +
            "sum(gc) grossConversions, " +
            "sum(conversion) conversions, " +
            "sum(sc) scrubbedConversions, " +
            "sum(caps) caps, " +
            "sum(aprice) accessPrice, " +
            "sum(pprice) putPrice " +
            " from " + tableName +
            " where offer_id = #{offerId} " +
            "group by symbol, affiliate_id")
    List<SummaryAffiliateData> summaryAffiliateDataByOffer(@Param("offerId") Integer offerId,
            @Param("sZone") String zone);
}
